package com.study.mall.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.study.mall.product.entity.CategoryEntity;
import com.study.mall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.common.utils.PageUtils;
import com.study.common.utils.Query;

import com.study.mall.product.dao.AttrGroupDao;
import com.study.mall.product.entity.AttrGroupEntity;
import com.study.mall.product.service.AttrGroupService;
import org.springframework.transaction.annotation.Transactional;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    CategoryService categoryService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        if (!StringUtils.isEmpty(key)) {
            wrapper.and((obj) -> {
                obj.eq("attr_group_id", key).or().like("attr_group_name", key);
            });

        }
        if (catelogId == 0) {

            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );

            return new PageUtils(page);
        } else {

            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper.eq("catelog_id", catelogId)
            );

            return new PageUtils(page);


        }
    }

    @Override
    public Long[] getCatelogPath(Long catelogId) {

        List<Long> path = new ArrayList<>();

        List<Long> groupPath = getGroupPath(catelogId, path);

        Collections.reverse(groupPath);


        return groupPath.toArray(new Long[groupPath.size()]);
    }

    private List<Long> getGroupPath(Long catelogId, List<Long> path) {
        path.add(catelogId);
        CategoryEntity category = categoryService.getById(catelogId);

        //获取此菜单的父id
        if (category.getParentCid() != 0) {
            //递归查找父id
            List<Long> groupPath = getGroupPath(category.getParentCid(), path);


        }
        return path;

    }
}

